package org.sendvox.freswitch.core.impl;

import org.jboss.netty.channel.Channel;
import org.sendvox.freswitch.core.Application;
import org.sendvox.freswitch.core.CommandException;
import org.sendvox.freswitch.core.SendMsg.CallCommand;
import org.sendvox.freswitch.core.SleepCommand;
import org.sendvox.freswitch.outbound.OutboundClient;
import org.sendvox.freswitch.util.Arguments;

/**
 * Pause the channel for a given number of milliseconds, consuming the audio for
 * that period of time.
 * 
 */
public class SleepCommandImpl implements SleepCommand {

	private OutboundClient outboundClient;
	private Integer milliseconds;

	public SleepCommandImpl(OutboundClient outboundClient) {
		this.outboundClient = outboundClient;
	}

	@Override
	public SleepCommand by(Integer milliseconds) {
		Arguments.INSTANCE.validate(milliseconds).isNotNull().isPositive()
				.isInRange(1, Integer.MAX_VALUE);
		this.milliseconds = milliseconds;
		return this;
	}

	@Override
	public SleepCommand execute(Channel channel) throws CommandException {
		outboundClient.sendmsg.callCommand(CallCommand.EXECUTE)
				.executeAppName(Application.SLEEP)
				.executeAppArg(milliseconds.toString()).execute(channel);
		return this;
	}

}
